using System;
using System.Collections;
using System.Collections.Generic;
using System.Html;
using System.Runtime.CompilerServices;
using Awaiter;
using Nsb.Classes;
using ThemeChain;
using Yoshi.Classes;
using jQueryApi;

namespace WhirliGig.Classes
{
    public partial class GigShowEdit
    {
        public void ImageItem(string url, int x, int y)
        {
            if (url.IndexOf(ThemeBase.BaseUri) == 0)
                url = url.Substr(ThemeBase.BaseUri.Length);

            GigEdit ge = null;
            GigData gd = new GigData(PageSelectTo);

            new Await()
                .AddDl(delegate(Await aw) { ImageManager.ThumbToFullAw(aw, url); })
                .AddDx(delegate(Await aw) { url = (string)aw.Result; })
                .AddDx(
                    delegate
                    {
                        gd.html = String.Format("<div><img class='GigC' src='{0}'></div>", url);
                        gd.classNames = gd.type = "DropImage";
                        gd.dim.width = 200;
                        InsertPos(gd, x - 100, y - 100);
                    })
                .AddDx(delegate { ge = (GigEdit)InjectIntoPage(gd, new GigEdit(this)); })
                .AddAw(SaveAllAw)
                .AddDx(delegate { ge.DelayScoopSize(url); })
                .AddDx(delegate { jQuery.Select(PageCssTo).FadeIn(300); })
                .Commit();
        }

        [AlternateSignature]
        public extern void TextItem();

        public void TextItem(string txt)
        {
            Inform.Event(Script.Literal("arguments"));
            Exceptions.SaveLog(ThemeBase.EventLogName, "GigTypes.TextItem()");
            GigData gd = new GigData(PageSelectTo);
            gd.html = String.Format("<div><div class='GigC'>{0}</div></div>", txt ?? GigEdit.YourText);
            gd.hasHeight = true;
            gd.classNames = gd.type = "TextItem";
            gd.css = (Dictionary) jQuery.Extend(
                new Dictionary(),
                Gig.DeefCss,
                new Dictionary(
                    "font-family", "Calibri",
                    "font-size", "14pt"
                    ));
            gd.dim.width = 270;
            gd.dim.height = 45;
            gd.autosize = true;
            NewPos(gd);

            new Await()
                .AddDx(delegate { InjectIntoPage(gd, new GigEdit(this)); })
                .AddDx(delegate { Editor.AutoSize(GigById(gd.id)); })
                .AddAw(Editor.SaveAllAw)
                .AddDx(delegate { jQuery.Select(PageCssTo).FadeIn(300); })
                .Commit();
        }

        public void HtmlOrderedList()
        {
            GigData gd = new GigData(PageSelectTo);
            gd.html = String.Format("<div><div class='GigC'><h3>Title</h3><ol><li>First line.</li></ol></div></div>");
            gd.hasHeight = true;
            gd.classNames = gd.type = "TextItem";
            gd.css = new Dictionary("font-family", "Calibri", "font-size", "16pt");
            gd.dim.width = 400;
            gd.dim.height = 45;
            gd.autosize = true;
            gd.listType = "ol";
            NewPos(gd);

            new Await()
                .AddDx(delegate { InjectIntoPage(gd, new GigEdit(this)); })
                .AddAw(Editor.SaveAllAw)
                .AddDx(delegate { jQuery.Select(PageCssTo).FadeIn(300); })
                .Commit();
        }

        public void InsertButton()
        {
            GigData gd = new GigData(PageSelectTo);
            gd.html = String.Format("<div><button class='GigC'>button</button></div>");
            gd.hasHeight = true;
            gd.classNames = gd.type = "Button";
            gd.css = new Dictionary("font-family", "Calibri", "font-size", "16pt");
            gd.dim.width = 80;
            gd.dim.height = 30;
            NewPos(gd);

            new Await()
                .AddDx(delegate { InjectIntoPage(gd, new GigEdit(this)); })
                .AddAw(Editor.SaveAllAw)
                .AddDx(delegate { jQuery.Select(PageCssTo).FadeIn(300); })
                .Commit();
        }

        [AlternateSignature]
        public extern void InsertTextBox();

        public void InsertTextBox(string type)
        {
            type = type ?? "text";
            GigData gd = new GigData(PageSelectTo);
            gd.html = String.Format("<div><input type='" + type + "' class='GigC'></div>");
            gd.hasHeight = true;
            gd.classNames = gd.type = "TextBox";
            gd.css = new Dictionary("font-family", "Calibri", "font-size", "16pt");
            gd.dim.width = 270;
            gd.dim.height = 45;
            NewPos(gd);

            new Await()
                .AddDx(delegate { InjectIntoPage(gd, new GigEdit(this)); })
                .AddAw(Editor.SaveAllAw)
                .AddDx(delegate { jQuery.Select(PageCssTo).FadeIn(300); })
                .Commit();
        }

        public void InsertPasswordBox()
        {
            InsertTextBox("password");
        }

        public void InsertTextareaBox()
        {
            GigData gd = new GigData(PageSelectTo);
            gd.html = String.Format("<div><textarea class='GigC'></textarea></div>");
            gd.hasHeight = true;
            gd.classNames = gd.type = "TextArea";
            gd.css = new Dictionary("font-family", "Calibri", "font-size", "16pt");
            gd.dim.width = 270;
            gd.dim.height = 45;
            NewPos(gd);

            new Await()
                .AddDx(delegate { InjectIntoPage(gd, new GigEdit(this)); })
                .AddAw(Editor.SaveAllAw)
                .AddDx(delegate { jQuery.Select(PageCssTo).FadeIn(300); })
                .Commit();
        }

        public void EmbeddedHtmlCode(string code)
        {
            GigData gd = new GigData(PageSelectTo);
            gd.html = "<div><div class='GigC'>" + code + "</div></div>";
            gd.hasHeight = true;
            gd.classNames = gd.type = "EmbeddedCode";
            int[] wh = DomElement.GetCodeDim(code);
            gd.dim.width = (int)((object)wh[0] ?? 410);
            gd.dim.height = (int)((object)wh[1] ?? 342);
            Inform.Debug("w={0}, h={1}", gd.dim.width, gd.dim.height);
            NewPos(gd);

            new Await()
                .AddDx(delegate { InjectIntoPage(gd, new GigEdit(this)); })
                .AddAw(Editor.SaveAllAw)
                .AddDx(delegate { jQuery.Select(PageCssTo).FadeIn(300); })
                .Commit();
        }

        public void AskEmbeddedCode()
        {
            new Await()
                .AddAw(new Ask().TextAreaAw, new Dictionary("Msg", "Enter embedded html code", "Value", ""))
                .AddAw(DomElement.RequireResultAw, DomElement.NotFailOrEmpty)
                .AddDx(delegate(Await aw) { EmbeddedHtmlCode((string)aw[ResultRx]); })
                .Commit();
        }

        public void InsertLinks()
        {
            Dictionary<string, Dictionary> ables = (Dictionary)Await.Files["insertables"];
            new Await()
                .AddDl(delegate(Await aw) { PasteAw(aw, (GigData)ables["gigs"]["Links box"]); })
                .AddAw(SaveAllAw)
                .AddDx(delegate { jQuery.Select(PageCssTo).FadeIn(300); })
                .Commit();
        }

        private void NewPos(GigData gd)
        {
            jQueryObject el = PageSelectToEl;
            jQueryPosition p = el.GetOffset();
            if (el.Length > 0)
            {
                int w = el.GetInnerWidth();
                gd.pos.Top = Math.Max(0, Document.Body.ScrollTop - p.Top) + 5;
                gd.pos.Left = (w / 2) - (gd.dim.width / 2);
            }
            else
            {
                gd.pos.Top = 100;
                gd.pos.Left = 300;
            }
            // GigDataOffFix
            //gd.off.Top = p.Top + gd.pos.Top;
            //gd.off.Left = p.Left + gd.pos.Left;
        }

        private void InsertPos(GigData gd, int x, int y)
        {
            jQueryPosition p = PageSelectToEl.GetOffset();
            gd.pos.Top = y /*+ Document.Body.ScrollTop*/- p.Top;
            gd.pos.Left = x /*+ Document.Body.ScrollLeft*/- p.Left;
            // GigDataOffFix
            //gd.off.Top = p.Top + Document.Body.ScrollTop + gd.pos.Top;
            //gd.off.Left = p.Left + Document.Body.ScrollLeft + gd.pos.Left;
            //Inform.Debug("InsertPost @ {0}x{1}", gd.off.Left, gd.off.Top);
        }
    }
}